﻿#include "record.h"
// 创建使用记录
Record create_record(User* user, Facility* facility, char* year, char* month, char* day, char* begin_time, char* end_time,bool is_end) {
    Record record;
    record.facility = facility;
    record.user = user;
    strncpy_s(record.year,5,year, 4);
    record.year[4] = '\0';
    strncpy_s(record.month, 3, month, 2);
    record.month[2] = '\0';
    strncpy_s(record.day, 3, day, 2);
    record.day[2] = '\0';
    strncpy_s(record.begin_time, 6, begin_time, 5);
    record.begin_time[5] = '\0';
    strncpy_s(record.end_time, 6, end_time, 5);
    record.end_time[5] = '\0';
    record.is_end = is_end;
    return record;
}
// 初始化使用记录链表
void init_REList(REList* list) {
    list->id = 0;
    list->num = 0;
    list->head = (RENode*)malloc(sizeof(RENode));
    if (list->head == NULL) {
        printf("内存分配失败！\n");
        return;
    }
    list->head->next = NULL;
}

// 创建使用记录节点
RENode* create_RENode(Record* record) {
    RENode* new_node = (RENode*)malloc(sizeof(RENode));
    if (new_node == NULL) {
        printf("内存分配失败！\n");
        return NULL;
    }
    new_node->record = *record;  // 把使用记录数据复制到新节点
    new_node->next = NULL;
    return new_node;
}

// 添加使用记录
void add_record(REList* list, Record* record) {
    RENode* new_node = create_RENode(record);
    new_node->record.record_id = list->id+1;
    RENode* temp = list->head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = new_node;
    list->num++;
    list->id++;
}

//// 删除使用记录节点
//int delete_record(REList* list, int user_id, int facility_id) {
//    if (list->head->next == NULL) {
//        printf("使用记录链表为空！\n");
//        return -1;
//    }
//    RENode* cur_node = list->head->next;
//    RENode* pre_node = NULL;
//    // 查找要删除的使用记录
//    while (cur_node != NULL && (cur_node->record.user->id != user_id || cur_node->record.facility->id != facility_id)) {
//        pre_node = cur_node;
//        cur_node = cur_node->next;
//    }
//    // 如果没有找到该使用记录
//    if (cur_node == NULL) {
//        printf("没有找到指定的使用记录！\n");
//        return -1;
//    }
//    // 删除节点
//    pre_node->next = cur_node->next;
//    free(cur_node);
//    list->num--;
//    return 0;
//}
//
//// 查找使用记录节点
//Record* find_record(REList* list, int user_id, int facility_id) {
//    RENode* temp = list->head->next;
//    while (temp != NULL) {
//        if (temp->record.user->id == user_id && temp->record.facility->id == facility_id) {
//            return &temp->record;
//        }
//        temp = temp->next;
//    }
//    return NULL;  // 没有找到使用记录
//}
//
// 打印链表中的所有使用记录
void print_records(REList* list) {
    if (list->head->next == NULL) {
        printf("使用记录链表为空！\n");
        return;
    }
    RENode* temp = list->head->next;  // 从头节点的下一个开始
    printf("使用记录列表：\n");
    while (temp != NULL) {
        printf("用户: %s, 场地ID: %d，%s年%s月%s日，%s-%s，记录状态：",
            temp->record.user->id_nicknames,
            temp->record.facility->id,
            temp->record.year,temp->record.month,temp->record.day,
            temp->record.begin_time,temp->record.end_time);
        if (temp->record.is_end) {
            printf("已结束\n");
        }
        else {
            printf("进行中\n");
        }
        temp = temp->next;
    }
}

// 打印场地信息
void print_record(RENode* renode) {
    RENode* temp = renode; 
    //printf("使用记录列表：\n");
    if(temp != NULL) {
        printf("用户: %s, 场地ID: %d，%s年%s月%s日，%s-%s，记录状态：",
            temp->record.user->id_nicknames,
            temp->record.facility->id,
            temp->record.year, temp->record.month, temp->record.day,
            temp->record.begin_time, temp->record.end_time);
        if (temp->record.is_end) {
            printf("已结束\n");
        }
        else {
            printf("进行中\n");
        }
    }
}
// 查找使用记录 用户Phone
void search_record_by_phone(System sys) {
    char phone[50] = { '\0' };
    int cnt = 0;
    printf(" 使用记录的用户电话：");
    scanf_s("%s", phone, 50);
    clear_input_buffer();
    RENode* cur_node = sys.record_link.head->next;
    while (cur_node != NULL) {
        if (strcmp(cur_node->record.user->phone,phone) == 0) {
            cnt++;
            print_record(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条使用记录\n", cnt);
}
// 查找使用记录 场地ID
void search_record_by_facility_id(System sys) {
    int facility_id = 0;
    int cnt = 0;
    printf(" 使用记录的场地ID：");
    scanf_s("%d", &facility_id);
    clear_input_buffer();
    RENode* cur_node = sys.record_link.head->next;
    while (cur_node != NULL) {
        if (cur_node->record.facility->id == facility_id) {
            cnt++;
            print_record(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条使用记录\n", cnt);
}
//查找使用记录 日期
void search_record_by_date(System sys) {
    char year[5] = { '\0' };
    char month[3] = { '\0' };
    char day[3] = { '\0' };
    int cnt = 0;
    printf(" 请输入日期（格式:YYYY年MM月DD日）:");
    int result = scanf_s("%4s年%2s月%2s日", year, 5, month, 3, day, 3);
    clear_input_buffer();
    RENode* cur_node = sys.record_link.head->next;
    while (cur_node != NULL) {
        if (strcmp(cur_node->record.year, year) == 0&&
            strcmp(cur_node->record.month,month)== 0&&
            strcmp(cur_node->record.day,day)==0){
            cnt++;
            print_record(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条使用记录\n", cnt);
}
//查找使用记录 日期 + 用户Phone
void search_record_by_phone_date(System sys) {
    char phone[50];
    char year[5] = { '\0' };
    char month[3] = { '\0' };
    char day[3] = { '\0' };
    int cnt = 0;
    printf(" 使用记录的用户电话：");
    scanf_s("%s", phone, 50);
    clear_input_buffer();
    printf(" 请输入日期（格式:YYYY年MM月DD日）:");
    int result = scanf_s("%4s年%2s月%2s日", year, 5, month, 3, day, 3);
    clear_input_buffer();
    RENode* cur_node = sys.record_link.head->next;
    while (cur_node != NULL) {
        if (strcmp(cur_node->record.user->phone,phone)==0 &&
            strcmp(cur_node->record.year, year) == 0 &&
            strcmp(cur_node->record.month, month) == 0 &&
            strcmp(cur_node->record.day, day) == 0) {
            cnt++;
            print_record(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条使用记录\n", cnt);
}
//查找使用记录 日期 + 场地ID
void search_record_by_facility_id_date(System sys) {
    int facility_id = 0;
    char year[5] = { '\0' };
    char month[3] = { '\0' };
    char day[3] = { '\0' };
    int cnt = 0;
    printf(" 使用记录的场地ID：");
    scanf_s("%d", &facility_id);
    clear_input_buffer();
    printf(" 请输入日期（格式:YYYY年MM月DD日）:");
    int result = scanf_s("%4s年%2s月%2s日", year, 5, month, 3, day, 3);
    clear_input_buffer();
    RENode* cur_node = sys.record_link.head->next;
    while (cur_node != NULL) {
        if (cur_node->record.facility->id==facility_id &&
            strcmp(cur_node->record.year, year) == 0 &&
            strcmp(cur_node->record.month, month) == 0 &&
            strcmp(cur_node->record.day, day) == 0) {
            cnt++;
            print_record(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 共找到%d条使用记录\n", cnt);
}


// 排序功能
//查看使用记录 Order by 日期
void sort_record_order_by_date(REList* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next) return;
    else if (!list->head->next->next)    return;
    else {
        bool swapped = true;
        RENode* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head->next;
            while (ptr->next != NULL) {
                Record cur = ptr->record;
                Record next = ptr->next->record;
                if (compare_date(cur.year, cur.month, cur.day,
                    next.year, next.month, next.day) > 0) {
                    ptr->record = next;
                    ptr->next->record = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}
//查看使用记录 Order by 编号
void sort_record_order_by_id(REList* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next) return;
    else if (!list->head->next->next)    return;
    else {
        bool swapped = true;
        RENode* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head->next;
            while (ptr->next != NULL) {
                Record cur = ptr->record;
                Record next = ptr->next->record;
                if (cur.record_id - next.record_id > 0) {
                    ptr->record = next;
                    ptr->next->record = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}
//查看使用记录 Order by 用户名称
void sort_record_order_by_name(REList* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next) return;
    else if (!list->head->next->next)    return;
    else {
        bool swapped = true;
        RENode* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head->next;
            while (ptr->next != NULL) {
                Record cur = ptr->record;
                Record next = ptr->next->record;
                if (strcmp(cur.user->name,next.user->name)>0) {
                    ptr->record = next;
                    ptr->next->record = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}
//查看使用记录 Order by 场地编号
void sort_record_order_by_facility_id(REList* list) {
    // 咩有和只有一个都不需要排序
    if (!list->head || !list->head->next) return;
    else if (!list->head->next->next)    return;
    else {
        bool swapped = true;
        RENode* ptr;
        while (swapped) {
            swapped = false;
            ptr = list->head->next;
            while (ptr->next != NULL) {
                Record cur = ptr->record;
                Record next = ptr->next->record;
                if (cur.facility->id - next.facility->id > 0) {
                    ptr->record = next;
                    ptr->next->record = cur;
                    swapped = true;
                }
                ptr = ptr->next;
            }
        }
    }
}

// 统计功能
// 使用记录总统计
void statistic_all_record(System sys) {
    int cnt = 0;
    int time = 0;
    float fee = 0;
    RENode* cur_node = sys.record_link.head->next;
    while (cur_node != NULL) {
        cnt++;
        int begin = index_time(cur_node->record.begin_time);
        cur_node->record.begin_time[2] = ':';
        int end = index_time(cur_node->record.end_time);
        cur_node->record.end_time[2] = ':';
        time += end - begin;
        fee += (end - begin) * cur_node->record.facility->price_per_half_hour;
        print_record(cur_node);
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息,总时长%d分钟,累积收费%.2f元\n", cnt,time*30,fee);
}
// 使用记录统计 by user
void statistic_record_by_user(System sys) {
    char phone[50] = { '\0' };
    printf(" 使用记录的用户电话：");
    scanf_s("%s", phone, 50);
    clear_input_buffer();
    int cnt = 0;
    int time = 0;
    float fee = 0;
    RENode* cur_node = sys.record_link.head->next;
    while (cur_node != NULL) {
        if (strcmp(cur_node->record.user->phone, phone) == 0) {
            cnt++;
            int begin = index_time(cur_node->record.begin_time);
            cur_node->record.begin_time[2] = ':';
            int end = index_time(cur_node->record.end_time);
            cur_node->record.end_time[2] = ':';
            time += end - begin;
            fee += (end - begin) * cur_node->record.facility->price_per_half_hour;
            print_record(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息,总时长%d分钟,累积收费%.2f元\n", cnt, time * 30, fee);
}
// 使用记录统计 by facility
void statistic_record_by_facility(System sys) {
    int facility_id = 0;
    printf(" 使用记录的场地ID：");
    scanf_s("%d", &facility_id);
    clear_input_buffer();
    int cnt = 0;
    int time = 0;
    float fee = 0;
    RENode* cur_node = sys.record_link.head->next;
    while (cur_node != NULL) {
        if (cur_node->record.facility->id == facility_id) {
            cnt++;
            int begin = index_time(cur_node->record.begin_time);
            cur_node->record.begin_time[2] = ':';
            int end = index_time(cur_node->record.end_time);
            cur_node->record.end_time[2] = ':';
            time += end - begin;
            fee += (end - begin) * cur_node->record.facility->price_per_half_hour;
            print_record(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息,总时长%d分钟,累积收费%.2f元\n", cnt, time * 30, fee);
}
// 使用记录统计 by date
void statistic_record_by_date(System sys) {
    char year[5] = { '\0' };
    char month[3] = { '\0' };
    char day[3] = { '\0' };
    printf(" 请输入日期（格式:YYYY年MM月DD日）:");
    int result = scanf_s("%4s年%2s月%2s日", year, 5, month, 3, day, 3);
    clear_input_buffer();
    int cnt = 0;
    int time = 0;
    float fee = 0;
    RENode* cur_node = sys.record_link.head->next;
    while (cur_node != NULL) {
        if (strcmp(cur_node->record.year, year) == 0 &&
            strcmp(cur_node->record.month, month) == 0 &&
            strcmp(cur_node->record.day, day) == 0) {
            cnt++;
            int begin = index_time(cur_node->record.begin_time);
            cur_node->record.begin_time[2] = ':';
            int end = index_time(cur_node->record.end_time);
            cur_node->record.end_time[2] = ':';
            time += end - begin;
            fee += (end - begin) * cur_node->record.facility->price_per_half_hour;
            print_record(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息,总时长%d分钟,累积收费%.2f元\n", cnt, time * 30, fee);
}
// 使用记录统计 by user facility
void statistic_record_by_user_facility(System sys) {
    char phone[50] = { '\0' };
    printf(" 使用记录的用户电话：");
    scanf_s("%s", phone, 50);
    clear_input_buffer();
    int facility_id = 0;
    printf(" 使用记录的场地ID：");
    scanf_s("%d", &facility_id);
    clear_input_buffer();
    int cnt = 0;
    int time = 0;
    float fee = 0;
    RENode* cur_node = sys.record_link.head->next;
    while (cur_node != NULL) {
        if (strcmp(cur_node->record.user->phone, phone) == 0&&
            cur_node->record.facility->id == facility_id) {
            cnt++;
            int begin = index_time(cur_node->record.begin_time);
            cur_node->record.begin_time[2] = ':';
            int end = index_time(cur_node->record.end_time);
            cur_node->record.end_time[2] = ':';
            time += end - begin;
            fee += (end - begin) * cur_node->record.facility->price_per_half_hour;
            print_record(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息,总时长%d分钟,累积收费%.2f元\n", cnt, time * 30, fee);
}
// 使用记录统计 日期范围
void statistic_record_by_date_range(System sys) {
    char year1[5] = { '\0' };
    char month1[3] = { '\0' };
    char day1[3] = { '\0' };
    char year2[5] = { '\0' };
    char month2[3] = { '\0' };
    char day2[3] = { '\0' };
    printf(" 请输入最早日期（格式:YYYY年MM月DD日）:");
    int result1 = scanf_s("%4s年%2s月%2s日", year1, 5, month1, 3, day1, 3);
    clear_input_buffer();
    printf(" 请输入最晚日期（格式:YYYY年MM月DD日）:");
    int result2 = scanf_s("%4s年%2s月%2s日", year2, 5, month2, 3, day2, 3);
    clear_input_buffer();
    int cnt = 0;
    int time = 0;
    float fee = 0;
    RENode* cur_node = sys.record_link.head->next;
    while (cur_node != NULL) {
        if (compare_date(cur_node->record.year, cur_node->record.month, cur_node->record.day,
            year1, month1, day1) >= 0 &&
            compare_date(cur_node->record.year, cur_node->record.month, cur_node->record.day,
                year2, month2, day2) <= 0) {
            cnt++;
            int begin = index_time(cur_node->record.begin_time);
            cur_node->record.begin_time[2] = ':';
            int end = index_time(cur_node->record.end_time);
            cur_node->record.end_time[2] = ':';
            time += end - begin;
            fee += (end - begin) * cur_node->record.facility->price_per_half_hour;
            print_record(cur_node);
        }
        cur_node = cur_node->next;
    }
    printf(" 合计%d条预订信息,总时长%d分钟,累积收费%.2f元\n", cnt, time * 30, fee);
}